"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("core-js/modules/es.promise.js");

require("core-js/modules/es.object.assign.js");

require("core-js/modules/es.string.includes.js");

require("core-js/modules/web.dom-collections.iterator.js");

require("../iconfont/iconfont.css");

require("../style/index.scss");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const _import = file => () => Promise.resolve("".concat('@/views' + file + '/index.vue')).then(s => _interopRequireWildcard(require(s))); //  为vuex添加global模块，存储全局下发的数据


function registerGlobalModule(store) {
  let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  if (!store || !store.hasModule) return; // 初始化数据

  const initState = _objectSpread({}, props.globalState);

  if (!store.hasModule('global')) {
    const globalModule = {
      namespaced: true,
      state: initState,
      actions: {
        // 子应用改变state并通知父应用
        setGlobalState(_ref, payload) {
          let {
            commit
          } = _ref;
          commit('setGlobalState', payload);
          commit('emitGlobalState', payload);
        },

        // 初始化，只用于mount时同步父应用的数据
        initGlobalState(_ref2, payload) {
          let {
            commit
          } = _ref2;
          commit('setGlobalState', payload);
        }

      },
      mutations: {
        setGlobalState(state, payload) {
          state = Object.assign(state, payload);
        },

        emitGlobalState(state) {
          if (props.setGlobalState) {
            props.setGlobalState(state);
          }
        }

      }
    };
    store.registerModule('global', globalModule);
  }
} //  为vuex添加routes模块,存储及修改动态路由数据


function registerRoutesModule(store) {
  if (!store || !store.hasModule) return;

  if (!store.hasModule('routes')) {
    const routesModule = {
      namespaced: true,
      state: {
        routesData: []
      },
      mutations: {
        UPDATE_ROUTER_DATA(state, payload) {
          state.routesData = payload;
        }

      }
    };
    store.registerModule('routes', routesModule);
  }
} // 封装路由数据


function getRoutes(store, props) {
  if (!props && !store) return;

  if (props.globalState && props.globalState.routers) {
    const routesData = props.globalState.routers;

    if (props.name) {
      const routes = [];

      for (let i = 0, length = routesData.length; i < length; i += 1) {
        const element = routesData[i];

        if (element.path && element.path.includes(props.name) && element.meta && element.meta.moduleName && element.meta.moduleName === props.name) {
          const path = element.path.slice(props.name.length + 1);
          element.path = path;
          element.component = _import(path);
          routes.push(element);
        }
      }

      store.commit('routes/UPDATE_ROUTER_DATA', routes);
    }
  }
}

function initGlobalState(store, props) {
  registerGlobalModule(store, props);
  registerRoutesModule(store);
  getRoutes(store, props);
}

var _default = {
  initGlobalState
};
exports.default = _default;